package com.stottlerhenke.simbionic.editor.gui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import com.stottlerhenke.simbionic.common.SIM_Constants;
import com.stottlerhenke.simbionic.editor.SimBionicEditor;

/**
 * SB_MenuBar contains the menu information for SimBionic, and is used by both
 * SimBionic and parent applications. This class should never create its own actions,
 * but just plug in actions from other classes to the UI components.
 * 
 * SimBionicFrame uses an instance of this class as the SimBionic menu.
 * 
 */
public class SB_MenuBar extends JMenuBar
{
    public SB_MenuBar()
    {
        super();
    }

    public void create(SimBionicEditor simbionic)
    {
        JMenu fileMenu = createFileMenu(simbionic);
        this.add(fileMenu);

        JMenu editMenu = createEditMenu(simbionic);
        this.add(editMenu);

        JMenu viewMenu = createViewMenu(simbionic);
        this.add(viewMenu);

        JMenu buildMenu = createBuildMenu(simbionic);
        this.add(buildMenu);

        JMenu helpMenu = createHelpMenu(simbionic);
        this.add(helpMenu);
    }

    public static JMenu createFileMenu(SimBionicEditor simbionic)
    {
        JMenu fileMenu = new JMenu("File");

        JMenuItem newItem = new JMenuItem(simbionic.newAction);
        newItem.setToolTipText(SimBionicEditor.NEW_COMMAND_TOOLTIP);
        fileMenu.add(newItem);

        JMenuItem openItem = new JMenuItem(simbionic.openAction);
        openItem.setToolTipText(SimBionicEditor.OPEN_COMMAND_TOOLTIP);
        fileMenu.add(openItem);

        JMenuItem saveItem = new JMenuItem(simbionic.saveAction);
        saveItem.setToolTipText(null);
        fileMenu.add(saveItem);

        JMenuItem saveAsItem = new JMenuItem(simbionic.saveAsAction);
        fileMenu.add(saveAsItem);

        fileMenu.addSeparator();

        JMenuItem importXMLItem = new JMenuItem(simbionic.importXMLAction);
        fileMenu.add(importXMLItem);

        fileMenu.add(createExportSubMenu(simbionic));

        fileMenu.addSeparator();

        JMenuItem exitItem = new JMenuItem(simbionic.exitAction);
        fileMenu.add(exitItem);
        return fileMenu;
    }

    public static JMenu createEditMenu(SimBionicEditor simbionic)
    {
        JMenu editMenu = new JMenu("Edit");

        JMenuItem undoItem = new JMenuItem(simbionic.undoAction);
        undoItem.setToolTipText(null);
        JMenuItem redoItem = new JMenuItem(simbionic.redoAction);
        redoItem.setToolTipText(null);
        JMenuItem cutItem = new JMenuItem(simbionic.cutAction);
        cutItem.setToolTipText(null);
        JMenuItem copyItem = new JMenuItem(simbionic.copyAction);
        copyItem.setToolTipText(null);
        JMenuItem pasteItem = new JMenuItem(simbionic.pasteAction);
        pasteItem.setToolTipText(null);
        JMenuItem deleteItem = new JMenuItem(simbionic.deleteAction);
        deleteItem.setToolTipText(null);
        editMenu.add(undoItem);
        editMenu.add(redoItem);
        editMenu.addSeparator();
        editMenu.add(cutItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        editMenu.add(deleteItem);
        editMenu.addSeparator();
        editMenu.add(simbionic.selectAllAction);
        editMenu.addSeparator();
        editMenu.add(simbionic.findAction);
        editMenu.add(simbionic.replaceAction);
        return editMenu;
    }

    public static JMenu createBuildMenu(SimBionicEditor simbionic)
    {
        JMenu buildMenu = new JMenu("Build");

        JMenuItem compileItem = new JMenuItem(simbionic.compileAction);
        compileItem.setToolTipText(null);
        buildMenu.add(compileItem);
        buildMenu.addSeparator();

        JMenuItem connectItem = new JMenuItem(simbionic.connectAction);
        buildMenu.add(connectItem);
        JMenuItem runItem = new JMenuItem(simbionic.runAction);
        buildMenu.add(runItem);
        JMenuItem buildBreakpointItem = new JMenuItem(simbionic.breakpointAction);
        buildMenu.add(buildBreakpointItem);
        buildMenu.addSeparator();

        buildMenu.add(simbionic.settingsAction);
        return buildMenu;
    }
    
    public static JMenu createHelpMenu(SimBionicEditor simbionic)
    {
        JMenu helpMenu = new JMenu("Help");

        JMenuItem aboutItem = new JMenuItem(new AbstractAction("About") {
            public void actionPerformed(ActionEvent arg0)
            {
                Object[] items = {
                        new JLabel("SimBionic Editor (Java) " + SIM_Constants.SYSTEM_INFO),
                        new JLabel("Copyright 2005-2014 Stottler Henke Associates, Inc.")
                };
                JOptionPane.showMessageDialog(null,items);  
            }            
        });
        helpMenu.add(aboutItem);
        
        return helpMenu;
    }

    public static JMenu createViewMenu(SimBionicEditor simbionic)
    {
        SB_ToolBar tb = ComponentRegistry.getToolBar();
        JMenu viewMenu = new JMenu("View");
        viewMenu.add(tb._backAction);
        viewMenu.add(tb._forwardAction);
        viewMenu.addSeparator();
        viewMenu.add(simbionic.prevErrorAction);
        viewMenu.add(simbionic.nextErrorAction);
        return viewMenu;
    }

    public static JMenu createExportSubMenu(SimBionicEditor mSimBionic)
    {
        JMenu exportMenu = new JMenu("Export");
        exportMenu.add(mSimBionic.exportHeaderAction);
        exportMenu.add(mSimBionic.exportJavaAction);
        exportMenu.add(mSimBionic.exportXMLAction);
        return exportMenu;
    }
}
